/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */

var swapPairs = function (head) {
    let pre = new ListNode(0, head)
    let cur = pre
    let next
    while (pre.next && pre.next.next) {
        next = head.next
        // 相邻节点换位置
        pre.next = next
        head.next = next.next
        next.next = head

        pre = head
        head = head.next
    }
    return cur.next
};


var fn = function (head, m, n) {
    let dummy = new ListNode(0, head)
    let p = dummy
    for (let i = 1; i < m; i++) {
        p = p.next
    }
    let lefthead = p
    let start = lefthead.next
    let pre = start
    let cur = pre.next
    for (let i = m; i < n; i++) {
        let next = cur.next
        cur.next = pre
        pre = cur
        cur = next
    }
    lefthead.next = pre
    start.next = cur
    return dummy.next
}